Skip to content

Big endian C codegen - #79

Merged
hit9 merged 4 commits into
hit9:masterfrom
mpspace-io:feature/FW-1041-big-endian-c-codegen
Jun 19, 2026
Merged

hit9 merged 4 commits into
hit9:masterfrom
mpspace-io:feature/FW-1041-big-endian-c-codegen

Conversation

@dcasnowdon

Copy link
Copy Markdown
Contributor

When compiling for a big-endian platform (TMS570 -- big endian ARM), we observed byte-swapping behaviour in encoder and decoder. The round-trip result encoding on a big endian platform and decoding on little endian, was byte-swapped. This fix changes the code emitted to use endian independent bit-shifts, rather than pointer math, to encode and decode.

It passes the CI tests, and we have tested that it resolves the problem on real hardware. We would love for this to be accepted upstream.

dcasnowdon and others added 4 commits June 16, 2026 14:52
…nter indexing

The encoder's ((unsigned char *)&(field))[fi] pattern accesses bytes via host
memory layout, which gives wrong values on big-endian targets (e.g. TMS570)
because fi=0 maps to the MSB instead of the LSB.

Replace both encoder and decoder items with explicit bit-shift arithmetic that
is endian-neutral:

  Encoder: ((unsigned_type)(chain) >> (fi*8 + shift)) & mask
  Decoder: chain |= (chain_type)(((unsigned)s[si] shift) & mask) << (fi*8))

The decoder always uses |= rather than = so it accumulates bytes without
clobbering already-written ones; a memset(m, 0, sizeof(*m)) at the top of
each generated Decode function provides the required zero baseline.
string.h is included in the generated .c file for optimization mode.

Regenerate C/CPP optimization-mode examples to reflect the new output.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@hit9

hit9 commented Jun 19, 2026

Copy link
Copy Markdown
Owner

Thanks.

I make a continuation on top of this PR on #81

@hit9
hit9 merged commit d117c05 into hit9:master Jun 19, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants